﻿using Autodesk.Revit.DB;
using CountTools;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace CountTools
{
    public partial class CodeNameResultForm : System.Windows.Forms.Form
    {
        private string path;

        public CodeNameResultForm(Dictionary<string, List<ElementId>> elementsList, string path)
        {
            InitializeComponent();
            this.path = path;
            this.elementsList = elementsList;
        }

        private void CodeNameRuleForm_Load(object sender, EventArgs e)
        {
            treeList1.Enabled = true;

            foreach (string key in elementsList.Keys)
            {
                string node1 = key.Split('$')[0];
                string node2 = key.Split('$')[1];
                string node3 = key.Split('$')[2];
                string node4 = key.Split('$')[3];

                TreeListNode nodeL1 = null;
                TreeListNode nodeL2 = null;
                if (IsContain(treeList1.Nodes, node1))
                {
                    nodeL1 = treeList1.Nodes.First(o => o.GetDisplayText(treeListColumn1) == node1);
                }
                else
                {
                    nodeL1 = treeList1.Nodes.Add(node1, "");
                }
                if (IsContain(nodeL1.Nodes, node2))
                {
                    nodeL2 = nodeL1.Nodes.First(o => o.GetDisplayText(treeListColumn1) == node2);
                }
                else
                {
                    nodeL2 = nodeL1.Nodes.Add(node2, "");
                }
                TreeListNode nodeL3 = nodeL2.Nodes.Add(node3, node4);
            }

            treeList1.ExpandAll();

        }

        private bool IsContain(TreeListNodes nodes, string node1)
        {
            bool flag = false;
            foreach (TreeListNode node in nodes)
            {
                string nodeName = node.GetDisplayText(treeListColumn1);
                if (nodeName == node1)
                {
                    return true;
                }
            }
            return flag;
        }

        private void treeList1_DoubleClick(object sender, EventArgs e)
        {
            if (treeList1.FocusedNode.Nodes.Count == 0 && treeList1.FocusedColumn.FieldName == "清单编码")
            {
                string typeName = treeList1.FocusedNode.GetDisplayText(treeListColumn1);
                string codeName = treeList1.FocusedNode.GetDisplayText(treeListColumn2);
                ChangeCodeName f3 = new ChangeCodeName(path, codeName);
                f3.label1.Text = f3.label1.Text + typeName;
                f3.ShowDialog();
                if (f3.DialogResult == DialogResult.OK)
                {
                    treeList1.FocusedNode.SetValue(treeListColumn2, f3.CodeName);
                }
            }
        }



        private void button2_Click(object sender, EventArgs e)
        {
            List<string> resultList = new List<string>();
            TreeListNodes nodeList = treeList1.Nodes;
            foreach (TreeListNode node in nodeList)
            {
                TreeListNodes nodeList2 = node.Nodes;
                foreach (TreeListNode node2 in nodeList2)
                {
                    TreeListNodes nodeList3 = node2.Nodes;
                    foreach (TreeListNode node3 in nodeList3)
                    {
                        resultList.Add(node3.GetDisplayText(treeListColumn1) + "$" + node3.GetDisplayText(treeListColumn2));
                    }
                }
            }

            ResultList = resultList;
            this.Close();
        }



        public List<string> ResultList { get; internal set; }

        private void treeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            if (e.Column == treeListColumn2)
            {
                if (e.CellValue.ToString() == "未知")
                {
                    e.Appearance.BackColor = System.Drawing.Color.Red;
                    e.Appearance.Options.UseBackColor = true;
                }
                else if (string.IsNullOrEmpty(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = System.Drawing.Color.White;
                    e.Appearance.Options.UseBackColor = true;
                }

                else
                {
                    string typeNme = e.Node.GetDisplayText(treeListColumn1);

                    foreach (string key in elementsList.Keys)
                    {
                        string node3 = key.Split('$')[2];
                        string node4 = key.Split('$')[3];
                        if (node3 == typeNme && e.CellValue.ToString() != node4)
                        {
                            e.Appearance.BackColor = System.Drawing.Color.Green;
                            e.Appearance.Options.UseBackColor = true;
                            break;
                        }
                    }
                }
            }
        }
    }


}
